home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amyboard / amiga / time.c < prev   
C/C++ Source or Header  |  1995-08-12  |  5KB  |  254 lines

  1. /**
  2. *** time.c -- AmyBoard clock functions
  3. ***
  4. *** ------------------------------------------------------------------------
  5. *** This program is free software; you can redistribute it and/or modify
  6. *** it under the terms of the GNU General Public License as published by
  7. *** the Free Software Foundation; either version 2 of the License, or
  8. *** (at your option) any later version.
  9. ***
  10. *** This program is distributed in the hope that it will be useful,
  11. *** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *** GNU General Public License for more details.
  14. ***
  15. *** You should have received a copy of the GNU General Public License
  16. *** along with this program; if not, write to the Free Software
  17. *** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *** ------------------------------------------------------------------------
  19. ***
  20. **/
  21.  
  22. /*** Includes section
  23. */
  24. #include "amyboard.h"
  25.  
  26. #include <exec/io.h>
  27. #include <devices/timer.h>
  28. /**/
  29.  
  30.  
  31.  
  32.  
  33.  
  34. /*** Variable section
  35. */
  36. struct timerequest *clockRequest    = NULL;
  37. struct MsgPort *clockPort           = NULL;
  38. BOOL clockRequestSent               = FALSE;
  39. ULONG clockSignal;
  40.  
  41. struct timerequest *loadRequest     = NULL;
  42. struct MsgPort *loadPort            = NULL;
  43. BOOL loadRequestSent                = FALSE;
  44. ULONG loadSignal;
  45.  
  46. ULONG timeSignals;
  47. /**/
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /*** AbortTimer function
  54. */
  55. void AbortTimer(struct timerequest *tr)
  56.  
  57. { AbortIO((struct IORequest *) tr);
  58.   WaitIO((struct IORequest *) tr);
  59.   SetSignal(0, 1 << tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
  60. }
  61. /**/
  62.  
  63.  
  64.  
  65.  
  66.  
  67. /*** TimeClose function
  68. ***
  69. ***  time module termination
  70. **/
  71. VOID TimeClose(VOID)
  72.  
  73. { if (clockRequestSent)
  74.   { AbortTimer(clockRequest);
  75.   }
  76.   if (loadRequestSent)
  77.   { AbortTimer(loadRequest);
  78.   }
  79.   if (clockRequest)
  80.   { CloseDevice((struct IORequest *) clockRequest);
  81.     DeleteIORequest(clockRequest);
  82.   }
  83.   if (clockPort)
  84.   { DeleteMsgPort(clockPort);
  85.   }
  86.   if (loadRequest)
  87.   { CloseDevice((struct IORequest *) loadRequest);
  88.     DeleteIORequest(loadRequest);
  89.   }
  90.   if (loadPort)
  91.   { DeleteMsgPort(loadPort);
  92.   }
  93. }
  94. /**/
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /*** TimeInit function
  101. ***
  102. ***  Time module initialization
  103. **/
  104. ULONG CreateTimeRequest(struct MsgPort **port, struct timerequest **req)
  105.  
  106. { ULONG signal = 0;
  107.  
  108.   if ((*port = CreateMsgPort()))
  109.   { signal = 1 << (*port)->mp_SigBit;
  110.     if ((*req = CreateIORequest(*port, sizeof(**req))))
  111.     { if (OpenDevice((STRPTR) "timer.device", UNIT_MICROHZ,
  112.              (struct IORequest *) *req, 0))
  113.       { DeleteIORequest(*req);
  114.     *req = NULL;
  115.       }
  116.     }
  117.   }
  118.   return signal;
  119. }
  120. VOID TimeInit(VOID)
  121.  
  122. { clockSignal = CreateTimeRequest(&clockPort, &clockRequest);
  123.   loadSignal = CreateTimeRequest(&loadPort, &loadRequest);
  124.  
  125.   if (!clockRequest  ||  !loadRequest)
  126.   { exit(10);
  127.   }
  128.  
  129.   timeSignals = clockSignal | loadSignal;
  130. }
  131. /**/
  132.  
  133.  
  134.  
  135.  
  136.  
  137. /*** TimeCallback function
  138. ***
  139. *** Called when one of the timers gets triggered.
  140. **/
  141. void TimeCallback(ULONG signals)
  142.  
  143. { if (signals & clockSignal)
  144.   { WaitIO((struct IORequest *) clockRequest);
  145.     clockRequestSent = FALSE;
  146.     DecrementClocks();
  147.   }
  148.   if (signals & loadSignal)
  149.   { WaitIO((struct IORequest *) loadRequest);
  150.     loadRequestSent = FALSE;
  151.     LoadGameLoop();
  152.   }
  153. }
  154. /**/
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /*** StartAnyTimer function
  161. */
  162. void StartAnyTimer(struct timerequest *tr, long millisec)
  163.  
  164. {
  165.   /**
  166.   ***  No real need for this "if", just to avoid one
  167.   ***  division, if possible.
  168.   **/
  169.   if (millisec > 999)
  170.   { tr->tr_time.tv_secs = millisec / 1000;
  171.     tr->tr_time.tv_micro = (millisec % 1000) * 1000;
  172.   }
  173.   else
  174.   { tr->tr_time.tv_secs = 0;
  175.     tr->tr_time.tv_micro = millisec * 1000;
  176.   }
  177.   tr->tr_node.io_Command = TR_ADDREQUEST;
  178.   SendIO((struct IORequest *) tr);
  179. }
  180. /**/
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. /*** StartClockTimer function
  188. */
  189. void StartClockTimer(long millisec)
  190.  
  191. {
  192.   if (clockRequestSent)
  193.   { DisplayFatalError("Internal error: Time request already sent.\n", 0, 10);
  194.   }
  195.   StartAnyTimer(clockRequest, millisec);
  196.   clockRequestSent = TRUE;
  197. }
  198. /**/
  199.  
  200.  
  201.  
  202.  
  203.  
  204. /*** StopClockTimer function
  205. */
  206. int StopClockTimer(void)
  207.  
  208. {
  209.   if (clockRequestSent)
  210.   { AbortTimer(clockRequest);
  211.     clockRequestSent = FALSE;
  212.     return(TRUE);
  213.   }
  214.   else
  215.   { return(FALSE);
  216.   }
  217. }
  218. /**/
  219.  
  220.  
  221.  
  222.  
  223.  
  224. /*** StartLoadGameTimer function
  225. */
  226. void StartLoadGameTimer(long millisec)
  227.  
  228. { if (loadRequestSent)
  229.   { DisplayFatalError("Internal error: Time request already sent.\n", 0, 10);
  230.   }
  231.   StartAnyTimer(loadRequest, millisec);
  232.   loadRequestSent = TRUE;
  233. }
  234. /**/
  235.  
  236.  
  237.  
  238.  
  239.  
  240. /*** StopLoadGameTimer function
  241. */
  242. int StopLoadGameTimer(void)
  243.  
  244. { if (loadRequestSent)
  245.   { AbortTimer(loadRequest);
  246.     loadRequestSent = FALSE;
  247.     return(TRUE);
  248.   }
  249.   else
  250.   { return(FALSE);
  251.   }
  252. }
  253. /**/
  254.